<?php
/**
 * Simple Light PHP Framework
 *
 * @copyright Marc PEPIN 2012
 * @author Marc PEPIN
 *
 */

defined('SLP_ROOT') or die("You've not access to this page");
 
/** 
* Simple Light Php Framework Database Class
* @package		Slp
* @author    	Marc PEPIN
*/ 

class Slp_Database {
	
		/**
		* PDO class
		* @var PDO
		*/
		
        private $pdo;


		/**
		* current driver
		* @var string
		*/
		
		private $driver;
		
		
        /**
		* Constructor 
		*/
		
        public function __construct() {
			
			$config =  Slp::getInstance()->loaded->Slp_Config;
			$this->driver = $config->get('driver');
			
			try {
			   $this->pdo = new PDO($this->driver.':dbname='.$config->get('db').';host='.$config->get('host'), $config->get('user'), $config->get('pass'));
			}
			catch (PDOException $e) {
					throw slp::getInstance()->loadClass("Slp_Exception", array($e->getMessage()));
			}
			
        }
		
		
		/**
		* Database select
		* @param string 
		* @param array
		* @param array
		*/
		public function select($table, $args = array(), $where = array()) {
			$sql =  (!empty($where)) ? 'SELECT #fields FROM #table WHERE #where' : 'SELECT #fields FROM #table';
			$values = array();	
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			foreach($args as $k => $v) {
				$args[$k] = $k;
			}
			$sql = Slp::getInstance()->loaded->Slp_Common->format($sql, array(
													'#table' => $table,
													'#fields' => implode(',  ', $args), 
													'#where' => $where[0] .' '. $where[1] .' :where'
												)
											);
			if ($where) 
				$args[$where[2]] = 'where';
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			foreach ($args as $key => $value) {
				$args[$value] = ":$key";	
				unset($args[$key]);
			}
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			return $this->query($sql, $args);
		}
		
		/**
		* Database insert
		* @param string 
		* @param array
		*/
		
		public function insert($table, $args = array()) {
			$sql = "INSERT INTO #table (#fields) VALUES(#values)";
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			$values = array();
			foreach ($args as $key => $value) 
				$values[":$value"] = $key;	
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($values);
			$sql = Slp::getInstance()->loaded->Slp_Common->format($sql, array(
													'#table' => $table,
													'#fields' => implode(', ', $args), 
													'#values' => implode(', ', $values)
												)
									);
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			return $this->query($sql, $args);
		}
        
		
		/**
		* Database update
		* @param string 
		* @param array
		* @param array
		*/
		public function update($table, $args = array(), $where = array()) {
			$sql =  (!empty($where)) ? 'UPDATE #table SET #fields WHERE #where' : 'UPDATE #table SET #fields';
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			$values = array();
			foreach ($args as $key => $value) 
				$values[$value] = "$value = :$value";	
			if ($where) {
				$args[$where[2]] = 'where';
				$where[2] = ":where";
			}
			$sql = Slp::getInstance()->loaded->Slp_Common->format($sql, array(
													'#table' => $table,
													'#fields' => implode(',  ', $values), 
													'#where' => implode(' ', $where)
												)
											);
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			foreach ($args as $key => $value) {
				$args[$value] = ":$key";	
				unset($args[$key]);
			}
			Slp::getInstance()->loaded->Slp_Common->arrayKeyRevert($args);
			return $this->query($sql, $args);
		}
		
		
		/**
		* Execute query
		* @param string
		* @param array
		*/
		
        public function query($sql, $args = NULL) {
			$q = $this->pdo->prepare($sql);
			if (!$q->execute($args))
					throw slp::getInstance()->loadClass("Slp_Exception", array(implode(',', $q->errorInfo())));
			return $q;
        }

}

?>